package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.logic.AlertLogic;
	import engine.logic.LogSendLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.MsgTipsLogic;
	import engine.ui.UIManage;
	import engine.util.FilterUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	public class BanksLogic
	{
		private static var _self:BanksLogic
		
		private var _myDis:BanksUI;
		private var _model:ModelLocator;
		private var _infoList:Array
		private var _bindManage:BindManage;
		private var _changeMax	:int=-1;
		private var _goldExchangeCount:int;
		private var _maxNum:int	=	10;
		//兑换一次可获得的铜钱
		private var _oneTimesGold : int;
		
		private var bribeRec : Array = [];
		
		private var roleVIPLevel : int;
		
		/**
		 *	龙晶币 
		 */		
		public static const MAGIC_GOIN	: int	=	4;
		/**
		 *	龙晶币兑换金币 
		 */
		public static const GET_GLOD 		: int	=	0;
		/**
		 *	龙晶币兑换功勋 
		 */
		public static const GET_EXPLOIT 	: int	=	8;
		/**
		 *	龙晶币兑换声望 
		 */
		public static const GET_REPUTE 	: int	=	10;
		
		private var _isBind:Boolean				=	false;
		/*****/
		private var _count:int ; 
		/**批量元宝数**/
		private var _ChangeMoeny:int ; 
		/***铜币数*****/
		private var _ChangeCoin:int ;
		public function BanksLogic()
		{
			_bindManage			=	BindManage.getInstance();
			_model				=	ModelLocator.getInstance();
			_myDis				=	BanksUI.getInstance();
		}
		public static function getInstance():BanksLogic
		{
			if(_self == null)
			{
				_self = new BanksLogic();
			}
			return _self;
		}
		/**
		 * 开始监听
		 */		
		public function addListener():void
		{
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_CAPITAL_BATCH_EXCHANGE),getGoldMax);
				
			SystemSender.getInstance().CustomMsgEx(CustomToServerFlag.CLIENT_CUSTOMMSG_NEED_SELF_CAPITAL_EXCHANGE,[MAGIC_GOIN,GET_GLOD]);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_NEED_SELF_CAPITAL_EXCHANGE),showInfo)
			_myDis.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE, closeWindowHandle);
			_myDis.sendBtn.addEventListener(MouseEvent.CLICK,sendMsg);
			
			_myDis.sendMaxBtn.addEventListener(MouseEvent.CLICK,sendMsg);
			_myDis.sendMaxBtn.addEventListener(MouseEvent.MOUSE_OVER,sendOverHandle); 
			_myDis.sendMaxBtn.addEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn1.addEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn1.addEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn1.addEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn2.addEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn2.addEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn2.addEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn3.addEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn3.addEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn3.addEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			if(!_isBind)
			{
				_isBind	=	true;
				_bindManage.BindProp(_model.scene.Role,"VIPLevel",this,"VipLevel");
				_bindManage.BindProp(_model.scene.Role,"GoldExchangeCount",this,"goldExchangeCount");
				_bindManage.BindRecord(_model.scene,"VIPCountRec",addVIPRec,removeVIPRec);
			}
		}
		/**
		 * 移除监听
		 */		
		public function removeListener():void
		{
			_model.removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_NEED_SELF_CAPITAL_EXCHANGE),showInfo)
			_myDis.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE, closeWindowHandle);
			_myDis.sendBtn.removeEventListener(MouseEvent.CLICK,sendMsg);
			
			_myDis.sendMaxBtn.removeEventListener(MouseEvent.CLICK,sendMsg);
			_myDis.sendMaxBtn.removeEventListener(MouseEvent.MOUSE_OVER,sendOverHandle);
			_myDis.sendMaxBtn.removeEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn1.removeEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn1.removeEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn1.removeEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn2.removeEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn2.removeEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn2.removeEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
			_myDis.drinkBtn3.removeEventListener(MouseEvent.CLICK,sendBribeMsg);
			_myDis.drinkBtn3.removeEventListener(MouseEvent.MOUSE_OVER,overDrink);
			_myDis.drinkBtn3.removeEventListener(MouseEvent.MOUSE_OUT,sendOutHandle);
			
		}
		/**
		 * 玩家的VIP等级 
		 * @param val
		 * 
		 */		
		public function set VipLevel(val:int):void
		{
			roleVIPLevel = val;
			if(bribeRec.length == 3)
			{
				setBtnVisible();
			}
			if(val >= 5)
			{
				_myDis.sendMaxBtn.visible	=	true;
				_myDis.sendBtn.x			=	(_myDis.windowBG.width - (_myDis.sendBtn.width + _myDis.sendMaxBtn.width + 20)) * .5;
			}
			else
			{
				_myDis.sendMaxBtn.visible	=	false;
				_myDis.sendBtn.x			=	(_myDis.windowBG.width - _myDis.sendBtn.width) * .5;
			}
			showChangeMax();
		}
		/**
		 * 以领过的次数 
		 * @param val
		 * 
		 */		
		public function set goldExchangeCount(val:int):void
		{
			_goldExchangeCount	=	val;
//			showChangeMax();
		}
		
		/**
		 *贿赂钱庄的 表 
		 * @param PropertiesName
		 * @param PropertiesValue
		 * @param data
		 * 
		 */
		private function addVIPRec(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			if(PropertiesValue[0] == 44)
			{
				bribeRec[0] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
			else if(PropertiesValue[0] == 45)
			{
				bribeRec[1] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
			else if(PropertiesValue[0] == 46)
			{
				bribeRec[2] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
		}
		
		private function removeVIPRec(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			if(PropertiesValue[0] == 44)
			{
				bribeRec[0] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
			else if(PropertiesValue[0] == 45)
			{
				bribeRec[1] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
			else if(PropertiesValue[0] == 46)
			{
				bribeRec[2] = PropertiesValue[2]+PropertiesValue[3]-PropertiesValue[1];
				if(bribeRec.length == 3)
				{
					setBtnVisible();
				}
			}
		}
		/**
		 * 当前的最大次数
		 */		
		private function showChangeMax():void
		{
//			_changeMax	=	getMaxNum(_model.scene.Role.getVipLevel()) - _goldExchangeCount;
			if(_changeMax<0)return ;
			_count = _changeMax ; 
			if(_changeMax<=0)
			{
				_changeMax = 0 ;
			}
			_myDis.haveLabel.htmlStrByBorder	=	StaticIni.getSwfLangStrVar("UIString_1000192",[_changeMax]);
			if(_changeMax < 10)
			{
				_myDis.sendMaxBtn.enabled	=	false;
			}
			else
			{
				_myDis.sendMaxBtn.enabled	=	true;
			}
			if(_myDis.visible&&_changeMax <= 0&&bribeRec[0]<=0&&bribeRec[1]<=0&&bribeRec[2]<=0)
			{
				_myDis.showOrHide();
				UIManage.getInstance().requestHideUI(_myDis);
			}
		}
		/**
		 * VIP等级对应的次数 
		 * @param val
		 * 
		 */		
		private function getMaxNum(val:int):int
		{
			var num:int	=	0;
			switch(val)
			{
				case 0:
					num	=	1
					break;
				case 1:
					num	=	3
					break;
				case 2:
					num	=	5
					break;
				case 3:
					num	=	7
					break;
				case 4:
					num	=	9
					break;
				case 5:
					num	=	30
					break;
				case 6:
					num	=	30
					break;
				case 7:
					num	=	50
					break;
				case 8:
					num	=	50
					break;
				case 9:
					num	=	100
					break;
				case 10:
					num	=	200
					break;
			}
			return num;
		}
		/**
		 * 关闭窗口 
		 * @param e
		 * 
		 */		
		private function closeWindowHandle(e:NxWindowEvent):void
		{
			_myDis.hide();
		}
		/**
		 * 显示置换信息 
		 * @param e
		 * 
		 */		
		private function showInfo(e:CustomMsgEvent):void
		{
			var myMagic:int			=	_model.scene.Role.getMagicCoin();
			
			_infoList				=	e.pList;
			_changeMax=_infoList.shift();
			//_myDis.haveLabel.htmlStrByBorder	=	StaticIni.getSwfLangStrVar("UIString_1000192",[_changeMax]);
//			if(_changeMax <= 0)
//			{
//				_myDis.sendBtn.enabled	=	_myDis.sendMaxBtn.enabled	=	false;
//			}
//			else
//			{
//				_myDis.sendBtn.enabled	=	_myDis.sendMaxBtn.enabled	=	true;
////				if(_changeMax < 10)
////				{
////					_myDis.sendMaxBtn.enabled	=	false;
////				}
//			}
			if(_infoList[1] <= 0)
			{
				_myDis.needLabel.htmlText	=	"";
			}
			else
			{
				if( _infoList[ 1 ]  > myMagic )
				{
					_myDis.needLabel.htmlText		=	StaticIni.getSwfLangStrVar( "UIStr_1001675",  [ StaticIni.getSwfLangStrVar( "UIStr_1001746",[ _infoList[ 1 ]])] );
				}else
				{
					_myDis.needLabel.htmlText		=	StaticIni.getSwfLangStrVar( "UIStr_1001675", [ _infoList[ 1 ] ] );
				}
			}
			_oneTimesGold = _infoList[ 3 ];
			_myDis.infoLabel.htmlText	=	StaticIni.getSwfLangStrVar( "UIStr_1001674", [ _infoList[ 3 ] ]);
			updateLabel()
			showChangeMax();
		}
		/**
		 * 刷新两个显示标签的坐标
		 */		
		private function updateLabel():void
		{
			_myDis.needLabel.x	=	(_myDis.windowBG.width - (_myDis.infoLabel.width + _myDis.needLabel.width))  * .5;
			_myDis.infoLabel.x	=	_myDis.needLabel.x + _myDis.needLabel.width;
		}
		/**
		 * 请求转换
		 * @param e
		 * 
		 */		
		private function sendMsg(e:MouseEvent):void
		{
			var target:DisplayObjectContainer	=	e.currentTarget	as DisplayObjectContainer;
			var str:String = ""
			switch(target)
			{
				case _myDis.sendBtn:
					if(_model.scene.Role.getMagicCoin() < _infoList[1])
					{
//						AlertUI.showAlert("","你的元宝不足!",AlertUI.OK);
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("97296")); 
					} else {
						if(_changeMax==_count&&_count>0)
						{
							SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_SELF_CAPITAL_EXCHANGE,_infoList[0], _infoList[2], 1 );
							LogSendLogic.getInstance().btnClickNumSend(22832)
							_count--;
							var _tipLogic:MsgTipsLogic = MsgTipsLogic.getRes(_myDis.sendBtn);
							 str = StaticIni.getSwfLangStrVar("97194",[_infoList[1], _infoList[3]]);
							_tipLogic.showInfo(str);
						}else if(_changeMax==_count&&_changeMax==_count&&_count==0)
						{
							var tipLogic:MsgTipsLogic = MsgTipsLogic.getRes(_myDis.sendBtn);
							 str = StaticIni.getSwfLangStr("UIString_1000473");
							 tipLogic.showInfo(str);
						}else
						{
//							var tipLogic_1:MsgTipsLogic = MsgTipsLogic.getRes(_myDis.sendBtn);
//							str = StaticIni.getSwfLangStr("有延迟请稍后兑换");
//							tipLogic_1.showInfo(str);
						}
						
					}
					break;
				case _myDis.sendMaxBtn:
					if(_model.scene.Role.getMagicCoin() < _ChangeMoeny)
					{
						AlertUI.showAlert("",StaticIni.getSwfLangStr("32002"),AlertUI.OK);
					}
					else
					{
						if(_changeMax < _maxNum)
						{
							
							NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000193"))
//							AlertUI.showAlert("",StaticIni.getSwfLangStr("UIString_1000193"),AlertUI.OK);
						}
						else
						{
							SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_SELF_CAPITAL_EXCHANGE,_infoList[0], _infoList[2], _maxNum );
							_tipLogic = MsgTipsLogic.getRes(_myDis.sendMaxBtn);
							str = StaticIni.getSwfLangStrVar("97194",[String(_ChangeMoeny),String(_ChangeCoin)]);
							_tipLogic.showInfo(str);
							
						}
					}
					LogSendLogic.getInstance().btnClickNumSend(22833)
					break;
			}
		}
		/**
		 * 显示信息
		 * @param e
		 * 
		 */		
		private function sendOverHandle(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CAPITAL_BATCH_EXCHANGE ,_infoList[0], _infoList[2],_maxNum);
			
			
			var getGold:int		=	(_model.scene.Role.getPlayerLevel() * 400 + 22400 ) * _maxNum;
//			var str:String		=	"";
//			if(_changeMax < _maxNum)
//			{
//				str	=	StaticIni.getSwfLangStr("UIString_1000191");
//			}
//			else
//			{
//				str	=	StaticIni.getSwfLangStrVar("UIString_1000190",[String(_ChangeMoeny),String(_ChangeCoin)]);
//			}
//			TipsMainLogic.instance.showString(str);
		}
		/**
		 * 获取需要的龙金币 
		 * @return 
		 * 
		 */		
		private function getNeedMagicCoin():int
		{
			var _needGold:int	=	0;
			var roleCount:int	=	_model.scene.Role.getGoldExchangeCount();
			for(var i:int = roleCount+ 1; i <=  roleCount+ _maxNum; i++)
			{
				_needGold	+= 2* i;
			}
			return _needGold;
		}
		/**
		 * 关闭信息
		 * @param e
		 * 
		 */		
		private function sendOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 * 获取 兑换元宝
		 */		
		private function getGoldMax(e:CustomMsgEvent):void
		{
			_ChangeMoeny = e.pList[0];
			_ChangeCoin = e.pList[2];
			TipsMainLogic.instance.hide();
			var str:String		=	"";
			if(_changeMax < _maxNum)
			{
				str	=	StaticIni.getSwfLangStr("UIString_1000191");
			}
			else
			{
				str	=	StaticIni.getSwfLangStrVar("UIString_1000190",[String(_ChangeMoeny),String(_ChangeCoin)]);
			}
			TipsMainLogic.instance.showString(str);
		}
		public function get changeMax():int 
		{
			return _changeMax ;
		}
		/**
		 *本地配置文件 贿赂限制和奖励 
		 * @param index
		 * @return 
		 * 
		 */		
		private function getBribe(index : int):Object
		{
			var obj : Object = StaticIni.getIniObj("BankBribe_"+index);
			return obj;
		}
		
		private function clickOKBtn(type:int):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,type);
		}
		
		private function sendBribeMsg(e:MouseEvent):void
		{
			var bribeType : int;
			var obj : Object;
			var helpStr : String;
			switch(e.currentTarget)
			{
				case _myDis.drinkBtn1:
					bribeType = 26;
					clickOKBtn(bribeType)
					break;
				case _myDis.drinkBtn2:
					bribeType = 27;
					obj = getBribe(2);
					helpStr = StaticIni.getSwfLangStrVar("UIString_1000721",[obj["NeedMagicCoin"],
						int(obj["NeedGoldCoinMin"])*_oneTimesGold,
						int(obj["NeedGoldCoinMax"])*_oneTimesGold,
						bribeRec[1]]);
					if(bribeRec[1]>0)
					{
						AlertUI.showAlert("",helpStr,AlertUI.OK|AlertUI.CANCEL,function(type:int, str:String, clickData:*):void
							{
								if(type == AlertUI.OK)
								{
									clickOKBtn(bribeType)
								}
							});
					}
					break;
				
				case _myDis.drinkBtn3:
					bribeType = 28;
					obj = getBribe(3);
					helpStr = StaticIni.getSwfLangStrVar("UIString_1000721",[obj["NeedMagicCoin"],
						int(obj["NeedGoldCoinMin"])*_oneTimesGold,
						int(obj["NeedGoldCoinMax"])*_oneTimesGold,
						bribeRec[2]]);
					if(bribeRec[2]>0)
					{
						AlertUI.showAlert("",helpStr,AlertUI.OK|AlertUI.CANCEL,function(type:int, str:String, clickData:*):void
						{
							if(type == AlertUI.OK)
							{
								clickOKBtn(bribeType)
							}
						});
					}
					break;
			}
			
			
		}
		
		private function overDrink(e:MouseEvent):void
		{
			var helpStr : String = StaticIni.getSwfLangStr("UIString_1000722");
			var bribeObj : Object;
			var times :　int;
			var isHaveCountArr : Array = [];
			switch(e.currentTarget)
			{
				case _myDis.drinkBtn1:
					if(bribeRec[0]<=0)
					{
						TipsMainLogic.instance.showMain(helpStr);
						return;
					}
					bribeObj = getBribe(1);
					times = bribeRec[0];
					break;
				case _myDis.drinkBtn2:
					if(bribeRec[1]<=0)
					{
						TipsMainLogic.instance.showMain(helpStr);
						return;
					}
					bribeObj = getBribe(2);
					times = bribeRec[1];
					break;
				case _myDis.drinkBtn3:
					if(bribeRec[2]<=0)
					{
						TipsMainLogic.instance.showMain(helpStr);
						return;
					}
					bribeObj = getBribe(3);
					times = bribeRec[2];
					break;
			}
			
			helpStr = StaticIni.getSwfLangStrVar("UIString_1000721",[bribeObj["NeedMagicCoin"],
				int(bribeObj["NeedGoldCoinMin"])*_oneTimesGold,
				int(bribeObj["NeedGoldCoinMax"])*_oneTimesGold,
				times]);
			TipsMainLogic.instance.showWideString(helpStr,160);
		}
		
		private function setBtnVisible():void
		{
			_myDis.depictMc.visible = roleVIPLevel>=getBribe(1)["NeedVIP"];
			_myDis.drinkBtn1.visible = roleVIPLevel>=getBribe(1)["NeedVIP"];
			_myDis.drinkBtn2.visible = roleVIPLevel>=getBribe(2)["NeedVIP"];
			_myDis.drinkBtn3.visible = roleVIPLevel>=getBribe(3)["NeedVIP"];
			
			if(bribeRec[0]<=0)
			{
				_myDis.drinkBtn1.filters =[FilterUtil.GrayColorFilter];
			}
			else
			{
				_myDis.drinkBtn1.filters = [];
			}
			if(bribeRec[1]<=0)
			{
				_myDis.drinkBtn2.filters =[FilterUtil.GrayColorFilter];
			}
			else
			{
				_myDis.drinkBtn2.filters = [];
			}
			if(bribeRec[2]<=0)
			{
				_myDis.drinkBtn3.filters =[FilterUtil.GrayColorFilter];
			}
			else
			{
				_myDis.drinkBtn3.filters = [];
			}
		}
	}
}